package com.hopes.bizz.video.service.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.log.annotation.SysLog;
import com.hopes.bizz.video.service.entity.VideoRegionEntity;
import com.hopes.bizz.video.service.service.VideoRegionService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 视频区域
 *
 * @author daijie
 * @date 2023-10-24 13:40:17
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/videoRegion")
@Tag(description = "VideoRegion", name = "视频区域管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class VideoRegionController {

    private final VideoRegionService videoRegionService;

    /**
     * 分页查询
     *
     * @param page        分页对象
     * @param videoRegion 视频区域
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('video_videoRegion_view')")
    public R getVideoRegionPage(Page page, VideoRegionEntity videoRegion) {
        LambdaQueryWrapper<VideoRegionEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.orderByAsc(VideoRegionEntity::getSortNum);
        return R.ok(videoRegionService.page(page, wrapper));
    }

    /**
     * 树
     *
     * @param videoRegion 视频区域
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/tree")
    @PreAuthorize("@pms.hasPermission('video_videoRegion_view')")
    public R getVideoRegionTree(VideoRegionEntity videoRegion) {
        return R.ok(videoRegionService.tree(videoRegion));
    }


    /**
     * 同步视频区域
     *
     * @return R
     */
    @Operation(summary = "同步视频区域", description = "同步视频区域")
    @SysLog("同步视频区域")
    @GetMapping("/sync")
    @PreAuthorize("@pms.hasPermission('video_videoRegion_add')")
    public R sync() {
        return R.ok(videoRegionService.sync());
    }


    /**
     * 通过id查询视频区域
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('video_videoRegion_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(videoRegionService.getById(id));
    }

    /**
     * 新增视频区域
     *
     * @param videoRegion 视频区域
     * @return R
     */
    @Operation(summary = "新增视频区域", description = "新增视频区域")
    @SysLog("新增视频区域")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('video_videoRegion_add')")
    public R save(@RequestBody VideoRegionEntity videoRegion) {
        return R.ok(videoRegionService.save(videoRegion));
    }

    /**
     * 修改视频区域
     *
     * @param videoRegion 视频区域
     * @return R
     */
    @Operation(summary = "修改视频区域", description = "修改视频区域")
    @SysLog("修改视频区域")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('video_videoRegion_edit')")
    public R updateById(@RequestBody VideoRegionEntity videoRegion) {
        return R.ok(videoRegionService.updateById(videoRegion));
    }

    /**
     * 通过id删除视频区域
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除视频区域", description = "通过id删除视频区域")
    @SysLog("通过id删除视频区域")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('video_videoRegion_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(videoRegionService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param videoRegion 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('video_videoRegion_export')")
    public List<VideoRegionEntity> export(VideoRegionEntity videoRegion) {
        return videoRegionService.list(Wrappers.query(videoRegion));
    }
}
